import 'package:memo/state/model/todo.dart';
import 'package:memo/state/model/work_record.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

final dbHelp = DbHelp();

const tableList = [
  workRecordSQL,
  todoSQL,
];

class DbHelp {
  factory DbHelp() => _getInstance();

  static DbHelp get instance => _getInstance();
  static DbHelp _instance;

  DbHelp._internal() {
    database = _openDb();
  }

  static DbHelp _getInstance() {
    if (_instance == null) {
      _instance = DbHelp._internal();
    }
    return _instance;
  }

  Future<Database> database;

  Future<Database> _openDb() async {
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'demo.db');
    return await openDatabase(path, version: 1,
        onCreate: (Database db, int version) async {
      for (var tableSql in tableList) {
        await db.execute(tableSql);
      }
    });
  }
}
